"""
copy from oss.common.http_utils
"""
from django.http import JsonResponse


class SuccessResponse(JsonResponse):
    """正常响应"""

    def __init__(self, content=None, *args, **kwargs):
        data = {"success": True}
        if content is not None:
            data["content"] = content
        super(SuccessResponse, self).__init__(data, *args, **kwargs)


class ErrorResponse(JsonResponse):
    """错误响应"""

    def __init__(self, err_code, errmsg, *args, **kwargs):
        """错误响应的 content 内容请使用 kwargs 中的 detail 传递进入"""
        data = {
            "success": False,
            "error": {"code": err_code, "message": errmsg}
            # content 不是必填项
            # 'content': None
        }
        detail = kwargs.pop("detail", None)
        if detail is not None:
            data["content"] = detail
        super(ErrorResponse, self).__init__(data, *args, **kwargs)
